from gordon.objects import Map
import pygame
from pygame.locals import *
from players import Player

class Ball:
	def __init__(self, loc):
		self.loc = loc

class Field:
	def __init__(self, screen, images):
		self.tile_size = (16,16)
		self.map_shape = (26,15)
		""" Map: 	Layer 1: field	
					Layer 2: deco(blood etc)		
					Layer 3: pathfinding (calc'd to make shortest path avoiding tackle zones.
										  the A* implimentation I am using allows weighting.)
		"""
		self.map = Map(self.map_shape, 3)
		self.map_rect = pygame.Rect( 10, 10, 
									self.tile_size[0]*self.map_shape[0],
									self.tile_size[1]*self.map_shape[1]
									)
		self.players = []
		self.ball = Ball((13,7))
		self.screen = screen
		self.images = images
		
	def add_stock_player(self, loc):
		self.players.append(Player(loc))
		
	def set_ball_loc(self, loc):
		self.ball.loc = loc
		
	def build_path_map(self):
		t_points = []
		for p in self.players:
			for l in p.get_adj():
				t_points.append(l)
		for tp in t_points:
			self.map.set_at(tp, 0)
		for tp in t_points:
			old = self.map.get_at(tp)
			new = old + 1
			self.map.set_at(tp, new)
	def check_for_player(self,loc):
		for p in self.players:
			if p.loc == loc:
				return p
				
	def draw_field(self):
		for y in range(15):
			for x in range(26):
				self.screen.blit(self.images['grass'], (	x*16+self.map_rect.x, 
															y*16+self.map_rect.y, 16,16
															))	
															
	def draw_players(self):
		for p in self.players:
			self.screen.blit(self.images['team1'], (	(p.loc[0])*16+self.map_rect.x, 
														(p.loc[1])*16+self.map_rect.y, 16,16
												))